﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace geetol.sms.core
{
    /// <summary>
    /// 配置文件
    /// </summary>
    public class config
    {
        /// <summary>
        /// 获取短信内容
        /// </summary>
        /// <returns></returns>
        public static List<entity.smsconfig> getSmsContent()
        {
            string s = ReadFile("smsconfig.txt");
            if (string.IsNullOrEmpty(s))
            {
                List<entity.smsconfig> list = new List<entity.smsconfig>();
                list.Add(new entity.smsconfig() { smskey = "新增会员", smsValue = "新增会员短信", smsSend = false });
                list.Add(new entity.smsconfig() { smskey = "会员消费", smsValue = "会员消费短信", smsSend = false });
                list.Add(new entity.smsconfig() { smskey = "会员储值", smsValue = "会员储值短信", smsSend = false });
                setSmsContent(list);
                return list;
            }
            return Serialize.FromJson<List<entity.smsconfig>>(s);
        }

        public static void setSmsContent(List<entity.smsconfig> list)
        {
            var s = Serialize.ToJson(list);
            WriterFile("smsconfig.txt", s);
        }

        /// <summary>
        /// 获取短信变量的键值对
        /// </summary>
        /// <returns></returns>
        public static entity.smsvar LoadSmsVar()
        {
            var s = ReadFile("smsvar.txt");
            if (string.IsNullOrEmpty(s)) //如果为空赋默认值
            {
                entity.smsvar m = new entity.smsvar();
                entity.smsvar.varitem addvip = new entity.smsvar.varitem();
                addvip.smskey = "新增会员";
                addvip.smsvar.Add("会员卡号", "card_id");
                addvip.smsvar.Add("会员类别", "card_type");
                addvip.smsvar.Add("会员姓名", "vip_name");
                addvip.smsvar.Add("会员性别", "vip_sex");
                addvip.smsvar.Add("会员手机", "vip_tel");
                addvip.smsvar.Add("会员余额", "card_balance");
                addvip.smsvar.Add("当前积分", "vip_integral");
                addvip.smsvar.Add("累积积分", "total_integral");
                addvip.smsvar.Add("会员创建时间", "oper_date");
                m.varlist.Add(addvip);

                entity.smsvar.varitem addvip1 = new entity.smsvar.varitem();
                addvip1.smskey = "会员储值";
                addvip1.smsvar.Add("储值时间", "oper_date");
                addvip1.smsvar.Add("储值金额", "card_cost");
                addvip1.smsvar.Add("会员卡号", "card_id");
                addvip1.smsvar.Add("会员类别", "card_type");
                addvip1.smsvar.Add("会员姓名", "vip_name");
                addvip1.smsvar.Add("会员性别", "vip_sex");
                addvip1.smsvar.Add("会员手机", "vip_tel");
                addvip1.smsvar.Add("会员余额", "card_balance");
                addvip1.smsvar.Add("当前积分", "vip_integral");
                addvip1.smsvar.Add("累积积分", "total_integral");
                m.varlist.Add(addvip1);

                entity.smsvar.varitem addvip2 = new entity.smsvar.varitem();
                addvip2.smskey = "会员消费";
                addvip2.smsvar.Add("消费时间", "oper_date");
                addvip2.smsvar.Add("消费金额", "card_cost");
                addvip2.smsvar.Add("会员卡号", "card_id");
                addvip2.smsvar.Add("会员类别", "card_type");
                addvip2.smsvar.Add("会员姓名", "vip_name");
                addvip2.smsvar.Add("会员性别", "vip_sex");
                addvip2.smsvar.Add("会员手机", "vip_tel");
                addvip2.smsvar.Add("会员余额", "card_balance");
                addvip2.smsvar.Add("当前积分", "vip_integral");
                addvip2.smsvar.Add("累积积分", "total_integral");
                m.varlist.Add(addvip2);

                var q = Serialize.ToJson(m);
                WriterFile("smsvar.txt", q);
                return m;
            }
            return Serialize.FromJson<entity.smsvar>(s);
        }


        static Dictionary<string, string> getFile(string fliename)
        {
            string s = ReadFile(fliename);
            if (string.IsNullOrEmpty(s))
                return new Dictionary<string, string>();
            else
                return Serialize.JsonToDict(s);
        }

        static void setFile(Dictionary<string, string> sVar, string filename)
        {
            string s = Serialize.ToJson(sVar);
            WriterFile(filename, s);
        }


        public static string ReadFile(string filename)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + filename;
            if (!File.Exists(path))
                return "";
            StreamReader sr = new StreamReader(path, Encoding.UTF8);
            string s = sr.ReadToEnd();
            sr.Close();
            return s;
        }

        public static void WriterFile(string filename, string s)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + filename;
            StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8);
            sw.Write(s);
            sw.Flush();
            sw.Close();
        }

        static string ReadDesFile(string filename)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + filename;
            if (!File.Exists(path))
                return "";
            StreamReader sr = new StreamReader(path, Encoding.UTF8);
            string s = sr.ReadToEnd();
            sr.Close();
            return Secret.DesDecrypt(s);
        }


        static void WriterDesFile(string filename, string s)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + filename;
            StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8);
            s = Secret.DesEncrypt(s);
            sw.Write(s);
            sw.Flush();
            sw.Close();
        }

        public static void SaveSqlConnection(string ip, string baseName, string user, string pwd)
        {
            string s = string.Format("Data Source={0};Initial Catalog={1};User ID={2};PassWord={3}", ip, baseName, user, pwd);
            WriterDesFile("conn.dll", s);
        }

        public static string LoadSqlConnection()
        {
            return ReadDesFile("conn.dll");
        }

        public static entity.sendconfig LoadSendConfig()
        {
            string s = ReadFile("sendconfig.txt");
            if (string.IsNullOrEmpty(s))
            {
                return new entity.sendconfig();
            }
            return Serialize.FromJson<entity.sendconfig>(s);
        }

        public static void SaveSendConfig(entity.sendconfig scon)
        {
            var s = Serialize.ToJson(scon);
            WriterFile("sendconfig.txt", s);
        }


        public static weimei_sms_api.api_info LoadApi_Info()
        {
            string s = ReadDesFile("wmapiinfo.dll");
            if (string.IsNullOrEmpty(s))
            {
                return null;
            }
            return Serialize.FromJson<weimei_sms_api.api_info>(s);
        }

        public static void SaveApi_Info(weimei_sms_api.api_info scon)
        {
            var s = Serialize.ToJson(scon);
            WriterDesFile("wmapiinfo.dll", s);
        }
    }
}
